/*
 * @Description: 公共方法
 * @Author: lijinpeng
 * @Date: 2020-07-26 22:51:18
 * @LastEditors: lijinpeng
 */ 

/**
* @descripttion: 校验是否为url
* @param {String} url
* @return:
*/
const isUrl = url => {
  return /^https?:\/\/(([a-zA-Z0-9_-])+(\.)?)*(:\d+)?(\/((\.)?(\?)?=?&?[a-zA-Z0-9_-](\?)?)*)*$/i.test(url)
}

/**
 * @descripttion: 通过URL下载文件
 * @param {String} url
 * @param {String} target
 * @return: 
 */
const download = (url, target) => {
  const a = document.createElement('a')
  a.href = url
  a.style.display = 'none'
  if (target) {
    a.target = target
  }
  document.body.appendChild(a)
  a.click()
  document.body.removeChild(a)
}

/**
 * @descripttion: 密码强度 验证
 * @param {String} password
 * @return: 
 */
const checkPwdStrong = password => {
  let modes = 0
  //正则表达式验证符合要求的
  if (password.length < 1) return modes
  if (/\d/.test(password)) modes += 25 //数字
  if (/[a-z]/.test(password)) modes += 25 //小写
  if (/[A-Z]/.test(password)) modes += 25 //大写
  if (/\W/.test(password)) modes += 25 //特殊字符
  //逻辑处理
  return modes
}

/**
 * @descripttion: 根据身份证号获取信息
 * @param {String} UUserCard
 * @param {Number} num 1:获取出生日期，2:获取性别 奇数男 偶数女，3:获取年龄
 * @return: string
 */
const getIdCardInfo = (UUserCard, num) => {
  if (num === 1) {
    //获取出生日期
    let birth
    if (UUserCard.length === 15) {
      let year = UUserCard.charAt(6) + UUserCard.charAt(7);//年份
      if (parseInt(year) < 14) year = `20${year}`//年份
      else year = `19${year}`//年份
      birth = `${year}-${UUserCard.substring(8, 10)}-${UUserCard.substring(10, 12)} 00:00:00`
    } else {
      birth = `${UUserCard.substring(6, 10)}-${UUserCard.substring(10, 12)}-${UUserCard.substring(12, 14)} 00:00:00`
    }
    return birth
  }
  if (num === 2) {
    //获取性别 奇数男 偶数女
    let sex = UUserCard.length === 15 ? parseInt(UUserCard.substr(14, 1)) : parseInt(UUserCard.substr(16, 1))
    // 余数为零代表女性、否则为男性
    if (sex % 2 == 1) {
      //男
      return '1'
    } else {
      //女
      return '2'
    }
  }
  if (num === 3) {
    //获取年龄
    var myDate = new Date()
    var month = myDate.getMonth() + 1
    var day = myDate.getDate()
    var age = myDate.getFullYear() - UUserCard.substring(6, 10) - 1
    if (UUserCard.length === 15) {
      if (UUserCard.substring(8, 10) < month || UUserCard.substring(8, 10) == month && UUserCard.substring(10, 12) <= day) {
        age++
      }
    } else {
      if (UUserCard.substring(10, 12) < month || UUserCard.substring(10, 12) == month && UUserCard.substring(12, 14) <= day) {
        age++
      }
    }
    return age
  }
}

export {
  isUrl,
  download,
  checkPwdStrong,
  getIdCardInfo
}
